﻿using AutoMapper;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using WMS.FC.Domain.DomainObject.System;
using WMS.FC.Domain.PersistenceObject.Business;
using WMS.FC.Domain.Repository;
using WMS.FC.Domain.Service.Material.Interface;
using WMS.FC.Domain.Service.System;

namespace WMS.FC.Domain.Service.Material
{
    public class GetMaterialByBindBarcodeService : ServiceBase, IGetMaterialByBindBarcodeService
    {
        protected readonly IMaterialRepository materialRepository;

        public GetMaterialByBindBarcodeService(
            ILogger<ServiceBase> logger,
            IOptions<WmsSettings> options,
            IMapper mapper,
            IMaterialRepository materialRepository)
            : base(logger, options, mapper)
        {
            this.materialRepository = materialRepository;
        }

        public async Task<ActionResult<MaterialPO>> GetCellMaterialByBindBarcode(string cellBindBarcode)
        {
            var result = new ActionResult<MaterialPO>("根据识别码获取电池物料");
            try
            {
                if (string.IsNullOrWhiteSpace(cellBindBarcode))
                    return result.ArgumentError("电池识别码不能为空");

                var materialPO = await materialRepository.GetMaterialByBindCode(new List<string>() { "MaterialType_Cell" }, cellBindBarcode);
                if (materialPO == null)
                    return result.BusinessError($"根据识别码:({cellBindBarcode}),无法获取电池物料");

                result.Data = materialPO;
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }

        public async Task<ActionResult<MaterialPO>> GetTrayMaterialByBindBarcode(string trayBindBarcode)
        {
            var result = new ActionResult<MaterialPO>("根据识别码获取托盘物料");
            try
            {
                if (string.IsNullOrWhiteSpace(trayBindBarcode))
                    return result.ArgumentError("托盘识别码不能为空");

                var materialPO = await materialRepository.GetMaterialByBindCode(new List<string>() { "MaterialType_Tray", "MaterialType_Tool" }, trayBindBarcode);
                if (materialPO == null)
                    return result.BusinessError($"根据识别码:({trayBindBarcode}),无法获取托盘物料");

                result.Data = materialPO;
            }
            catch (Exception ex)
            {
                return result.Exception(ex);
            }

            return result;
        }
    }
}
